package com.sctvcloud.renhe.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.R;
import com.ruihang.generalibrary.ui.util.IAutoPlayAction;
import com.ruihang.ijkplaylib.bean.IPlayClarity;
import com.ruihang.ijkplaylib.listener.OnStatusChanged;
import com.ruihang.ijkplaylib.widget.IGPlayerConstructorCallback;
import com.ruihang.ijkplaylib.widget.PlayStateParams;
import com.sctvcloud.renhe.ui.widget.BasePlayerView;
import com.shuyu.gsyvideoplayer.listener.INetPromt;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;

import java.util.List;

import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by panxd on 2016/9/14.
 */
public class FloatGSYVideoUtils {

    private Context context;

    private Activity activity;

    private View targetView;//用以确定playerview位置的view（竖屏时)

    private AbsoluteLayout.LayoutParams flParam;
    private AbsoluteLayout.LayoutParams tempParam;
    private AbsoluteLayout videoLayout;


    private long currentPostion;

    private int statusHeight;

    private int parentWidth, parentHeight, parentX, parentY;

    private String currentPath;

    private boolean isStop = false;

    private boolean isAboutStatusHeiht = true;

    private BasePlayerView playerView;

    private boolean isBackValued;

    private View backView;

    private int backViewColor;

    private int otherHeight;
    /**
     * 竖屏时，parentview所在viewgroup的Y偏移量
     */
    private int scrolledY;
    /**
     * 如果为true，则播放区域不能滑出屏幕或超出屏幕外
     */
    private boolean isShowFull;

    private boolean isPlayerAutoPlay;//播放器是否自动隐藏控制条

    private List<IPlayClarity> claritiesTemp;//清晰度缓存

    private boolean shouldSetClaryty;

    private View.OnClickListener playerMenuClick;

    private View.OnClickListener playerNextClick;
    //    private WoPlayerView.IFullScreenClick fullScreenClick;
//
    private boolean isPause;//是否暂停中

    private boolean isPlayed;//是否已经开始了播放

    private int topMode;
    /**
     * 是否只在顶部显示
     */
    private boolean isTop;

    private Handler handler;
    /**
     * 是否是直播
     */
    private boolean isLiving;

    private boolean isPortrait = true;//是否是竖屏

    public static final int TOPMODE_DEF = 0;
    public static final int TOPMODE_LIVING = 1;
    public static final int TOPMODE_DEMAND = 2;

//    private SpeedSdkHelper speedSdkHelper;

    private IPlayerFactor playerFactor;

    private float tempY;

    private boolean hasInited;//是否已经对playerview进行了init

    private ScrollTemp scrollTemp = new ScrollTemp();//用于滑动缓存等

    private SampleListener sampleListener = new SampleListener() {
        @Override
        public void onPrepared(String url, Object... objects) {
            super.onPrepared(url, objects);
            isPlayed = true;
        }
    };
    protected IMediaPlayer.OnInfoListener infoListener;
    protected IMediaPlayer.OnCompletionListener completionListener;
    protected IMediaPlayer.OnPreparedListener preparedListener;

    public void setOninfoListener(IMediaPlayer.OnInfoListener infoListener) {
        this.infoListener = infoListener;
    }

    public void setOnCompletionListener(IMediaPlayer.OnCompletionListener completionListener) {
        this.completionListener = completionListener;
    }

    public void setOnPreparedListener(IMediaPlayer.OnPreparedListener preparedListener) {
        this.preparedListener = preparedListener;
    }

    private RecyclerView.OnScrollListener recyclerScrollListener = new RecyclerView.OnScrollListener() {

        private int stateTemp;

        private boolean tempDown;//注意：手指往上滑动是自动向下播放

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            stateTemp = newState;
//            JLog.e("scroll state:" + newState);
//            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                if (isAutoPlayNext && mediaView.getVisibility()==View.GONE && autoPlay != null) {
//                    autoPlay.autoNext(parentView,tempDown);
//                }
//            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
//            JLog.e("eeae  dy:" + dy + "  py:" + playerView.getY() + " pt :" + videoLayout.getScrollY());
            recyclerScrolled(dx, dy);
        }
    };

    public void recyclerScrolled(int dx, int dy) {
        JLog.e("qqwq   in recycler scrolled dx=" + dx + "  dy=" + dy);
        if (dx == 0 || dy == 0) {
            return;
        }
        if (playerView != null && playerView.getVisibility() == View.VISIBLE) {
            int sy = videoLayout.getScrollY();
            boolean tempDown = dy > 0;//是否上滑
            videoLayout.scrollBy(dx, dy);
            float cy = sy - playerView.getY();
            if (tempDown) {
                if (cy - playerView.getHeight() >= 0) {
                    JLog.e("qqwq  will stop video");
                    stopVideoView(false);
                }
//                    if(sy <= mh + my+8) {
//                        JLog.e("pull up mediaview: x:"+mediaView.getX()+"  y:"+mediaView.getY()+" h:"+mediaView.getHeight()
//                                + "  lay scroll y:"+videoLayout.getScrollY());

//                        if (videoLayout.getScrollY() - mediaView.getY() >= mh / 2) {
//                            stopVideoView();
//                            if (isAutoPlayNext) {
//                                if (stateTemp != RecyclerView.SCROLL_STATE_SETTLING && Math.abs(dy) < 20) {
//                                    if (autoPlay != null) {
//                                        if (tempDown) {
//                                            autoPlay.autoNext(parentView, true);
//                                        } else {
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }

            } else {
//                    if (sy>=my-videoLayout.getHeight()-8) {
//                        JLog.e("pull down mediaview: x:"+mediaView.getX()+"  y:"+mediaView.getY()+" h:"+mediaView.getHeight()
//                                + "  lay scroll y:"+videoLayout.getScrollY());
//                    }
                if (videoLayout.getHeight() + cy <= 0) {
                    JLog.e("qqwq   will stop video");
                    stopVideoView(false);
                }
            }
        }
    }

    public void setPlayed(boolean played) {
        isPlayed = played;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    public void onConfigurationChanged(Activity ac, Configuration newConfig, OrientationUtils orientationUtils) {
        JLog.e("qqwq  or=" + newConfig.orientation + "   isPlayed=" + isPlayed);
        if (isPlayed) {
            playerView.onConfigurationChanged(ac, newConfig, orientationUtils);
        }
    }

    public void setPlayerView(BasePlayerView playerView) {
        this.playerView = playerView;
    }

    public void showPlayer(View targeView, boolean orientationEnable) {
        if (videoLayout == null) {
            return;
        }
        if (!isTop && targeView == null) {
            JLog.e("target view is null and top is false");
            return;
        }

        this.targetView = targeView;
        if (playerView == null) {
            initFloatView();
        } else {
            if (flParam == null) {
                flParam = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0);
            }
            flParam.width = targetView.getWidth();
            flParam.height = this.targetView.getHeight();
            if (!isTop) {
                int[] location = new int[2];
                this.targetView.getLocationInWindow(location);
                flParam.x = location[0];
//                flParam.y = location[1] ;
                flParam.y = location[1] - statusHeight - otherHeight;
            } else {
                flParam.x = 0;
                flParam.y = 0;
            }
//				mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_FIT_PARENT, 0);
            if (ViewCompat.isAttachedToWindow(playerView)) {
                videoLayout.updateViewLayout(playerView, flParam);
            } else {
                videoLayout.addView(playerView, flParam);
                hasInited = true;
            }
        }

    }

    public FloatGSYVideoUtils(Context context, View parentView, AbsoluteLayout videoLayout, int otherHeight) {
        this.context = context;
//        this.isBackValued = isBackValued;
//        speedSdkHelper = new SpeedSdkHelper();
        handler = new Handler(Looper.getMainLooper());
        this.otherHeight = otherHeight;
        if (context instanceof Activity) {
            activity = (Activity) context;
        }
        String powerTag = this.getClass().getName();
        if ((context instanceof Activity)) {
            powerTag = context.getClass().getName();
        }
//        this.wakeLock=powerManager.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,powerTag);
//        this.wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, powerTag);
        this.targetView = parentView;
        this.videoLayout = videoLayout;
        this.statusHeight = getStatusHeight();
    }

    public void setCurrentPostion(long currentPostion) {
        this.currentPostion = currentPostion;
    }

    private void init(List<? extends IPlayClarity> list, int pos, boolean playNow) {
        if (isPortrait) {
            flParam = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0);
            flParam.width = targetView.getWidth();
            flParam.height = targetView.getHeight();
            JLog.e("isTOp=" + isTop);
            if (!isTop) {
                int[] location = new int[2];
                targetView.getLocationInWindow(location);

                flParam.x = location[0];
//                flParam.y = location[1] ;
                flParam.y = location[1] - statusHeight - otherHeight;
            }
        } else {
            flParam = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.MATCH_PARENT, AbsoluteLayout.LayoutParams.MATCH_PARENT, 0, 0);
        }
        if (pos >= list.size()) {
            pos = 0;
        }
        if (playerView == null) {
///
            initFloatView();

            if (playerView.getVisibility() == View.GONE) {
                playerView.setVisibility(View.VISIBLE);
            }
//            initVideo(mediaView, path, currentPostion);

            if (playNow) {
//                playerView.setPlaySource(list).switchStream(pos).startPlay();
            } else {
//                playerView.hideBottonBar(true);
//                playerView.hideCenterPlayer(false);
//                playerView.setAutoPlay(false);
//                playerView.hideHideTopBar(false);
//                playerView.switchStream(pos);
            }

        } else {

            videoLayout.updateViewLayout(playerView, flParam);
//            mVideoView.stopPlayback();//
//            mVideoView.setVideoURI(Uri.parse(path));//
//                    logE("NewsDetailActivity","initVideoView has set videouri ->\n"+path);
//            mVideoView.start();//
//                    logE("NewsDetailActivity","initVideoView has call start");
            if (playerView.getVisibility() == View.GONE) {
                playerView.setVisibility(View.VISIBLE);
            }
            if (playNow) {
//                playerView.setPlaySource(list).switchStream(pos).startPlay();
            } else {
//                playerView.switchStream(pos);
            }
        }
    }

    private void init(String path, String title, boolean playNow) {
        flParam = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0);
        flParam.width = targetView.getWidth();
        flParam.height = targetView.getHeight();
        if (!isTop) {
            int[] location = new int[2];
            targetView.getLocationInWindow(location);

            flParam.x = location[0];
//                flParam.y = location[1] ;
            flParam.y = location[1] - statusHeight - otherHeight;
        }
        if (playerView == null || !hasInited) {
///
            initFloatView();

            if (playerView.getVisibility() == View.GONE) {
                playerView.setVisibility(View.VISIBLE);
            }
//            initVideo(mediaView, path, currentPostion);


        } else {

            videoLayout.updateViewLayout(playerView, flParam);
//            mVideoView.stopPlayback();//
//            mVideoView.setVideoURI(Uri.parse(path));//
//                    logE("NewsDetailActivity","initVideoView has set videouri ->\n"+path);
//            mVideoView.start();//
//                    logE("NewsDetailActivity","initVideoView has call start");
        }

        if (playerView.getVisibility() == View.GONE) {
            playerView.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.equals(path, playerView.getOriUrl())) {
            playerView.setUp(path, false, title);
        }
        if (playNow) {
            playerView.startPlay();
        }
//                myData.setmParentView(parentView);
//                myData.setVideoInfo(path);
//                myData.setmMediaView(mediaView);
    }

    private void initFloatView() {
        if (playerView == null) {
//            mediaView = LayoutInflater.from(context).inflate(R.layout.activity_news_detail_video, null);
//            playerView = LayoutInflater.from(context).inflate(R.layout.gplayer_base, null);
            if (playerFactor != null) {
                playerView = playerFactor.createPlayerView();
            }
            if (playerView == null) {
                playerView = new BasePlayerView(context);
            }

            playerView.setStandardVideoAllCallBack(sampleListener);
            playerView.setOninfoListener(infoListener);
            playerView.setOnCompletionListener(completionListener);
            playerView.setOnPreparedListener(preparedListener);

//            JLog.e("" + (mediaView.getId() == R.id.gplayer_lay_base));
//            if (isBackValued) {
//                if (backView == null) {
//                    backView = new View(context);
//                    backView.setBackgroundColor(backViewColor == 0 ? Color.WHITE : backViewColor);
//                }
//                AbsoluteLayout.LayoutParams alp = new AbsoluteLayout.LayoutParams(flParam);
////                JLog.e("back x:" + alp.x + " y:" + alp.y + " w:" + alp.width);
//                alp.x = 0;
//                alp.y = 0;
//            }
//            videoLayout.addView(mediaView, flParam);
//            playerView = new GPlayerView((Activity) context, mediaView, constructorCallback)
//                    .setOnStatusChanged(this).setLive(isLiving).setAutoPlay(true).forbidTouch(false).setSeekCompleteListener
//                            (seekCompleteListener).setOnInfoListener(this);
//                    .setScaleType(PlayStateParams.f16_9).setOnNetPrompt(defNetPrompt)
//                    .setForbidHideControlPanl(false).Demand()
//                    .setFullScreenListener(playerViewFullListerner).setPanlShowingListener(defPanlListener)
//                    .setPlayingTimeListener(playingTimeListener);
//            if (playerMenuClick != null) {
//                playerView.setMenuClickListener(defPlayerMenuClick);
//            }
//            setTopMode(topMode);
//            if (shouldSetClaryty) {
//                setClarities(claritiesTemp);
//                shouldSetClaryty = false;
//            }
            playerView.setVisibility(View.GONE);
        }
        if (targetView != null) {
            if (flParam == null) {
                flParam = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.MATCH_PARENT, AbsoluteLayout.LayoutParams.MATCH_PARENT, 0, 0);
            }
            flParam.width = targetView.getWidth();
            flParam.height = targetView.getHeight();

            int[] location = new int[2];
            targetView.getLocationInWindow(location);
            flParam.x = location[0];
//                flParam.y = location[1] ;
            flParam.y = location[1] - statusHeight - otherHeight;
//				mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_FIT_PARENT, 0);
            tempY = flParam.y;
            if (hasInited) {
                videoLayout.updateViewLayout(playerView, flParam);
            } else {
                videoLayout.addView(playerView, flParam);
                hasInited = true;
            }
        }

        if (playerView != null) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (context.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                        if (targetView != null) {
                            parentHeight = targetView.getHeight();
                            parentWidth = targetView.getWidth();
                            int[] location = new int[2];
                            targetView.getLocationInWindow(location);
                            parentX = location[0];
                            parentY = location[1];
//                        Log.e("parentView onChange before", "parentX:" + parentX + " parentY:" + parentY);
                        }
                    }
                }
            }, 200);
        }
    }

    public FloatGSYVideoUtils setLiving(boolean living) {
        isLiving = living;
        if (playerView != null) {
            playerView.setLiving(isLiving);
        }
        return this;
    }

    public void setPlayerFactor(IPlayerFactor playerFactor) {
        this.playerFactor = playerFactor;
    }

    public void play(View parentView, String url, String title, boolean now) {
        JLog.e("url:" + url);
        if (parentView == null || TextUtils.isEmpty(url)) {
            return;
        }
//        if(isPlaying()){
        stopVideoView(false);
//        }
        this.targetView = parentView;
        videoLayout.scrollTo(0, 0);

        init(url, title, now);
    }

//    public FloatVideoUtils setLogin(boolean login) {
//        isLogin = login;
//        return this;
//    }

    public void scrollBy(int dx, int dy) {
        videoLayout.scrollBy(dx, dy);
    }

    public void scrollTo(int dx, int dy) {
        videoLayout.scrollTo(dx, dy);
    }

    public boolean isPlayed() {
        return isPlayed;
    }

    public boolean isShowing() {
        return playerView != null && playerView.getVisibility() == View.VISIBLE;
    }

    public boolean isHasInited() {
        return hasInited;
    }
//    public boolean hasPrepared() {
//        return playerView != null && playerView.hasPrepared();
//    }

    public boolean isShowControlPanl() {
//        return playerView != null && playerView.isShowControlPanl();
        return false;
    }

    public void setClarities(List<IPlayClarity> clarities) {
        JLog.e("clarities::" + clarities);
        claritiesTemp = clarities;
        if (playerView == null) {
            shouldSetClaryty = true;
        } else {
            if (clarities != null && clarities.size() > 0) {
//                List<VideoijkBean> list = new ArrayList<>();
//                for (int i = 0; i < clarities.size(); i++) {
//                    WoClarity wc = clarities.get(i);
//                    if (wc != null) {
//                        VideoijkBean vb = new VideoijkBean();
//                        vb.setStream(wc.getName());
//                        vb.setUrl(wc.getUrl());
//                        list.add(vb);
//                    }
//                }
//                playerView.setPlaySource(clarities);
            }
        }
    }

    public void setClarities(List<IPlayClarity> clarities, int defPos) {
        JLog.e("clarities::" + clarities);
        claritiesTemp = clarities;
        if (playerView == null) {
            shouldSetClaryty = true;
        } else {
            if (clarities != null && clarities.size() > 0) {
//                playerView.setPlaySource(clarities, defPos);
            }
        }
    }

    public void startPlay() {
        if (playerView == null) {
            JLog.e("Can't start play because player view is null");
            return;
        }
        if (playerView.getVisibility() == View.GONE) {
            playerView.setVisibility(View.VISIBLE);
        }
//        playerView.setAutoPlay(isPlayerAutoPlay);
        playerView.startPlay();
    }

    public void startPlay(Integer position) {
        JLog.e("ondemand position=" + position);
        if (playerView == null) {
            JLog.e("Can't start play because player view is null");
            return;
        }
        if (playerView.getVisibility() == View.GONE) {
            playerView.setVisibility(View.VISIBLE);
        }
//        playerView.setAutoPlay(isPlayerAutoPlay);
        if (position != null && position > 0) {
            JLog.e("ondemand will seek to " + position);
//            playerView.seekTo(position);
            playerView.setPlayPosition(position);
        }
        playerView.startPlay();
    }

//    public FloatVideoUtils setClarityChangedListener(IClarityChanged clarityChangedListener) {
//        if (playerView != null) {
//            playerView.setClarityChanged(clarityChangedListener);
//        }
//        return this;
//    }

    public boolean isPlaying() {
        return playerView != null && playerView.getCurrentState() == PlayStateParams.STATE_PLAYING;
//        return playerView != null && playerView.isPlaying();
//        return false;
    }

    public boolean isStoped() {
//        return playerView == null || playerView.getStatus() == PlayStateParams.STATE_IDLE;
        return false;
    }

    public List<IPlayClarity> getClaritiesTemp() {
        return claritiesTemp;
    }

//    public FloatVideoUtils hideListLay(boolean isHide) {
//        if (playerView != null) {
//            playerView.hideListLay(isHide);
//        }
//        return this;
//    }

//    public FloatVideoUtils setTopMode(int topMode) {
//        this.topMode = topMode;
//        if (playerView != null) {
//            if (topMode == TOPMODE_LIVING) {
//                playerView.Living();
//            } else if (topMode == TOPMODE_DEMAND) {
//                playerView.Demand();
//            } else {
//                playerView.defaultTopAdapter();
//            }
//        }
//        return this;
//    }

//    public FloatViedioUtils setLoginViewVisible(int visible) {
//        if (playerView != null) {
//            playerView.setLoginViewVisible(visible);
//        }
//        return this;
//    }

    public FloatGSYVideoUtils setScrolledY(int scrolledY) {
        this.scrolledY = scrolledY;
        return this;
    }

    public FloatGSYVideoUtils setShowFull(boolean showFull) {
        isShowFull = showFull;
        return this;
    }

    public FloatGSYVideoUtils setBackValued(boolean backValued) {
        isBackValued = backValued;
        return this;
    }

    public FloatGSYVideoUtils setBackViewColor(int backViewColor) {
        this.backViewColor = backViewColor;
        return this;
    }

    public GSYBaseVideoPlayer getPlayerView() {
        return playerView;
    }

    public boolean onBackPress() {
        if (playerView != null && hasInited) {
//            return playerView.onBackPressed();
            if (StandardGSYVideoPlayer.backFromWindowFull(context)) {
                return true;
            }
        }
        return false;
    }

    public void setSampleListener(SampleListener sampleListener) {
        if (sampleListener != null) {
            if (playerView != null && hasInited) {
                if (this.sampleListener != sampleListener) {
                    sampleListener.setInternal(this.sampleListener);
                    playerView.setStandardVideoAllCallBack(sampleListener);
                }
            } else {
                if (this.sampleListener != sampleListener) {
                    sampleListener.setInternal(this.sampleListener);
                    this.sampleListener = sampleListener;
                }
            }
        }
    }

//    public void showThumbnail(OnShowThumbnailListener showThumbnailListener) {
//        if (showThumbnailListener != null) {
//            playerView.showThumbnail(showThumbnailListener);
//        }
//    }

    public RecyclerView.OnScrollListener getRecyclerScrollListener() {
        return recyclerScrollListener;
    }

    public void refreshVideoLocation() {//刷新播放器位置
        if (targetView != null) {
            int[] location = new int[2];
            targetView.getLocationInWindow(location);
            if (parentWidth <= 0) {
                parentWidth = targetView.getWidth();
            }
            if (parentHeight <= 0) {
                parentHeight = targetView.getHeight();
            }
            flParam.width = parentWidth;
            flParam.height = parentHeight;

            flParam.x = location[0];
            flParam.y = location[1] - statusHeight - otherHeight;
            videoLayout.setClickable(false);
//                        videoLayout.scrollTo(scrollTemp.dx,scrollTemp.dy);

        }
        if (playerView != null) {
            videoLayout.scrollTo(0, 0);
            JLog.e("will update view layout x=" + flParam.x + "  y=" + flParam.y);
            videoLayout.updateViewLayout(playerView, flParam);
        }
    }

//    public void onConfigurationChangedEnd(Configuration newConfig) {
//        if (playerView != null) {
//            playerView.toggleProcessDurationOrientation(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
//        }
//    }

    public void onChangeToPortraitLandscape() {
        isPortrait = false;
        if (playerView != null) {
//            playerView.setPortrait(false);
        }

    }

    public void onPause() {
        GSYBaseVideoPlayer p = getCurPlayer();
        if (p != null && hasInited) {
            p.onVideoPause();
        }
        isPause = true;
//        speedSdkHelper.stop(context);
    }

    public void onDestory() {
        playerFactor = null;
        playerMenuClick = null;
        playerNextClick = null;
        GSYBaseVideoPlayer p = getCurPlayer();
        if (p != null) {
            p.release();
        }

        context = null;
        activity = null;
        playerView = null;
        System.gc();
    }

    private GSYBaseVideoPlayer getCurPlayer() {
        if (playerView != null) {
            if (playerView.getFullWindowPlayer() != null) {
                return playerView.getFullWindowPlayer();
            }
            return playerView;
        }
        return null;
    }

    public void onResume() {
        GSYBaseVideoPlayer p = getCurPlayer();
        if (p != null && hasInited) {
            p.onVideoResume();
//            if (!playerView.getPlayBtnEnable()) {
//                playerView.pausePlay();
//            }
        } else {
            JLog.e("bbab  p=" + p + "  has inited=" + hasInited);
        }
        isPause = false;
    }

    public void pausePlay() {
        GSYBaseVideoPlayer p = getCurPlayer();
        if (p != null && hasInited) {
            p.onVideoPause();
        }
//        speedSdkHelper.stop(context);
    }

    public void stopVideoView(boolean release) {
        if (playerView != null) {
//            playerView.stopPlay();
//            if (mediaView != null && mediaView.getVisibility() == View.GONE) {
//                playerView.callPlayerHide();
//            }
            if (release) {
                playerView.release();
            } else {
                playerView.onVideoPause();
            }
            playerView.setVisibility(View.GONE);
            if (playerView.getStandardVideoAllCallBack() != null) {
                if (playerView.getStandardVideoAllCallBack() instanceof SampleListener) {
                    ((SampleListener) playerView.getStandardVideoAllCallBack()).onPlayerHiddened();
                }
            }
        }
        if (videoLayout != null && videoLayout.getScrollX() != 0) {
            videoLayout.scrollTo(0, 0);
        }
//        speedSdkHelper.stop(context);
    }

    private void showVideView() {
        if (isStop) {
        }
    }

//    public FloatVideoUtils setPlayingTimeListener(WoPlayerView.IPlayingTimeListener playingTimeListener) {
//        this.playingTimeListener = playingTimeListener;
//        if (playerView != null) {
//            playerView.setPlayingTimeListener(playingTimeListener);
//        }
//        return this;
//    }

//    public FloatVideoUtils setNetPrompt(WoPlayerView.OnNetPrompt netPrompt) {
//        this.netPrompt = netPrompt;
//        return this;
//    }

//    public FloatViedioUtils setFullScreenClick(WoPlayerView.IFullScreenClick fullScreenClick) {
//        this.fullScreenClick = fullScreenClick;
//        return this;
//    }

//    public FloatViedioUtils setPanlShowingListener(WoPlayerView.IPanlShowingListener panlShowingListener) {
//        this.panlShowingListener = panlShowingListener;
//        return this;
//    }

//    public FloatVideoUtils setPlayerMenuClick(View.OnClickListener playerMenuClick) {
//        if (playerMenuClick == null) {
//            if (playerView != null) {
//                playerView.setMenuClickListener(null);
//            }
//        } else {
//            if (playerView != null) {
//                playerView.setMenuClickListener(defPlayerMenuClick);
//            }
//        }
//        this.playerMenuClick = playerMenuClick;
//        return this;
//    }

//    public FloatViedioUtils setPlayerNextClick(View.OnClickListener playerNextClick) {
//        if (playerNextClick == null) {
//            if (playerView != null) {
//                playerView.setNextClickListener(null);
//            }
//        } else {
//            if (playerView != null) {
//                playerView.setNextClickListener(defPlayerNextClick);
//            }
//        }
//        this.playerNextClick = playerNextClick;
//        return this;
//    }

    public FloatGSYVideoUtils setOtherHeight(int otherHeight) {
        this.otherHeight = otherHeight;
        return this;
    }

//    public WoPlayerView.IPlayingTimeListener getPlayingTimeListener() {
//        return playingTimeListener;
//    }

//    public boolean isUnPlayingByEnable() {
//        return playerView != null && !playerView.getPlayBtnEnable();
//    }

    /**
     * 获得状态栏的高度
     *
     * @param
     * @return
     */
    public int getStatusHeight() {

        int statusHeight = -1;
        try {
            Class clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = activity.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("getStatusHeight", "getStatusHeight:" + statusHeight);
        return statusHeight;
    }

    public View getTargetView() {
        return targetView;
    }

    public void setTargetView(View parentView) {
        this.targetView = parentView;
    }

//    public CustomMediaController getMediaController() {
//        return mediaController;
//    }

//    public int getTopHorHeight() {
//        int re = 0;
//        if (playerView != null) {
//            re = playerView.getTopHorHeight();
//        }
//        return re;
//    }

//    public void setMediaController(CustomMediaController mediaController) {
//        this.mediaController = mediaController;
//    }

    public boolean isAboutStatusHeiht() {
        return isAboutStatusHeiht;
    }

    public void setAboutStatusHeiht(boolean aboutStatusHeiht) {
        isAboutStatusHeiht = aboutStatusHeiht;
    }


    private class ScrollTemp {
        protected int dx;
        protected int dy;

        protected void temp() {
            if (videoLayout != null) {
                dx = videoLayout.getScrollX();
                dy = videoLayout.getScrollY();
            }
        }
    }

    public interface IPlayerFactor {
        BasePlayerView createPlayerView();
    }
}
